import { Injectable } from '@nestjs/common';
import { PrismaService } from '@/common/service/prisma/prisma.service';
import { Word, WordDto } from '../dto';
import { Table } from '../../table/dto';
@Injectable()
export class WordService {
    constructor(private prisma: PrismaService) { }

    async getTable() {
        const resp: any[] = await this.prisma.$queryRawUnsafe(`
                SELECT table_name FROM information_schema.tables WHERE table_schema = DATABASE();
                `)
        const result: Table[] = resp.map((e, i) => {
            return { id: i, name: e.TABLE_NAME }
        })
        const filter: Table[] = []
        const sys: string[] = [
            'front_account', 'gen_table', 'gen_table_column', 'sys_config', 'sys_dept', 'sys_dict_data', 'sys_dict_type', 'sys_logininfor', 'sys_menu', 'sys_notice', 'sys_post', 'sys_role', 'sys_role_dept', 'sys_role_menu', 'sys_user', 'sys_user_post', 'sys_user_role',
        ]
        result.forEach(element => {
            if (!sys.includes(element.name)) {
                filter.push(element)
            }
        });

        const fianlResult = filter.map((e, i) => { return { value: e.name, label: e.name } })

        return fianlResult
    }
    // 列出所有的表
    async getWord(name: string) {
        const resp: Word[] = await this.prisma.$queryRawUnsafe(`
                select * from \`${name}\`
                `)
        return resp
    }

    // 查找表
    async findWord(name: string, id: number) {
        const resp: Word[] = await this.prisma.$queryRawUnsafe(`
                SELECT * FROM \`${name}\` where id = '${id}';
                `)
        return resp[0]
    }
    // 更新表
    async updateWord(name: string, word: Word) {

        const query = `
        UPDATE ${name}
        SET eng = '${word.eng.replace(/'/g, "''")}', chi = '${word.chi.replace(/'/g, "''")}'
        WHERE id = ${word.id};
    `;
        const resp: any = await this.prisma.$executeRawUnsafe(query);
        return resp;
    }

    // 添加表
    async addWord(name: string, word: WordDto) {
        console.log(name, word);

        const query = `INSERT INTO ${name} (eng, chi) VALUES ('${word.eng}', '${word.chi}')`;
        const resp: any = await this.prisma.$executeRawUnsafe(query);

        return resp
    }

    // 删除表
    async delWord(name: string, id: number) {

        const query =`DELETE FROM ${name} WHERE id = '${id}' ;`

        const resp: any = await this.prisma.$executeRawUnsafe(query)
        // console.log(resp);

        return resp
    }
    // 查找单词

}
